package com.kob.backend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kob.backend.mapper.DueHistoryMapper;
import com.kob.backend.pojo.DueHistroy;
import com.kob.backend.service.user.DueHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DueHistoryServicelmpl implements DueHistoryService {
    @Autowired
    private DueHistoryMapper dueHistoryMapper;

    /*
    * 保存历史记录
    * */
    @Override
    public int save(DueHistroy histroy){return dueHistoryMapper.insert(histroy);}

    /*
    * 取消座位预约后，更新数据库
    * */
    @Override
    public int setCancel(DueHistroy histroy) {
        // 构建查询条件，按照预约时的时间倒序排序，限制结果集为1条
        QueryWrapper<DueHistroy> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userid", histroy.getUserid()) // 按照用户ID匹配
                .eq("date", histroy.getDate()) // 按照日期匹配
                .eq("time", histroy.getTime()) // 按照时间匹配
                .orderByDesc("duetime") // 按照预约时的时间倒序排序
                .last("LIMIT 1"); // 限制结果集为1条记录

        // 查找符合条件的最近一条记录
        DueHistroy latestRecord = dueHistoryMapper.selectOne(queryWrapper);
        // 如果找到记录，更新其 iscancel 字段为 1
        if (latestRecord != null) {
            latestRecord.setIscancel(1);
            return dueHistoryMapper.updateById(latestRecord);
        } else {
            // 如果未找到符合条件的记录，返回 0 或其他合适的错误码，表示更新操作未执行
            return 0;
        }
    }

    // 签到时，修改issignin字段
    @Override
    public int setSignin(DueHistroy histroy) {
        // 查找记录
        QueryWrapper<DueHistroy> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userid", histroy.getUserid());
        queryWrapper.eq("seatid", histroy.getSeatid());
        queryWrapper.eq("date", histroy.getDate());
        queryWrapper.eq("time", histroy.getTime())
                .orderByDesc("duetime") // 按照预约时的时间倒序排序
                .last("LIMIT 1"); // 限制结果集为1条记录

        DueHistroy latestRecord = dueHistoryMapper.selectOne(queryWrapper);
        if(latestRecord != null)
        {
            latestRecord.setIssignin(1);
            return dueHistoryMapper.updateById(latestRecord);
        }
        else{
            return 0;
        }
    }

    // 预约是否已签到
    @Override
    public boolean seatHasSignIn(int seatId, String userid, String selectedDateSlot, String selectedTimeSlot) {
        QueryWrapper<DueHistroy> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userid", userid);
        queryWrapper.eq("seatid", seatId);
        queryWrapper.eq("date", selectedDateSlot);
        queryWrapper.eq("time", selectedTimeSlot)
                .orderByDesc("duetime") // 按照预约时的时间倒序排序
                .last("LIMIT 1"); // 限制结果集为1条记录

        DueHistroy histroy = dueHistoryMapper.selectOne(queryWrapper);
        return histroy != null && histroy.getIssignin() == 1;
    }

    // 暂定  // 通过userid来获取对应userid的所有信息
    @Override
    public List<DueHistroy> findByUserId(String userId) {
        return null;
    }

    // 获取所有未进行签到也没有取消的记录
    @Override
    public List<DueHistroy> findPastDueHistory() {
        QueryWrapper<DueHistroy> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("issignin", 0)
                .eq("iscancel", 0)
                .orderByDesc("duetime");
        return dueHistoryMapper.selectList(queryWrapper);
    }

    // 获取签到、未取消的记录
    @Override
    public List<DueHistroy> findSignInAndNCancelHistory() {
        QueryWrapper<DueHistroy> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("issignin", 1)
                .eq("iscancel", 0)
                .orderByDesc("duetime");
        return dueHistoryMapper.selectList(queryWrapper);
    }
}

